<template>
    <view class="container">
        <!-- 充值金额 -->
        <view class="amount-section">
            <view class="section-title">充值金额</view>
            <view class="amount-input">
                <text class="currency">¥</text>
                <input 
                    type="digit"
                    v-model="amount"
                    placeholder="请输入充值金额"
                />
            </view>
            <view class="amount-list">
                <view 
                    class="amount-item"
                    v-for="(item, index) in amountOptions"
                    :key="index"
                    :class="{ active: amount == item }"
                    @tap="selectAmount(item)"
                >
                    ¥{{ item }}
                </view>
            </view>
        </view>

        <!-- 支付方式 -->
        <view class="payment-section">
            <view class="section-title">支付方式</view>
            <view class="payment-list">
                <view 
                    class="payment-item"
                    v-for="(item, index) in paymentMethods"
                    :key="index"
                    :class="{ active: selectedPayment === item.id }"
                    @tap="selectPayment(item.id)"
                >
                    <image :src="item.icon" mode="aspectFit"></image>
                    <text>{{ item.name }}</text>
                    <view class="check-icon"></view>
                </view>
            </view>
        </view>

        <button 
            class="submit-btn"
            :disabled="!isValid"
            :loading="submitting"
            @tap="handleRecharge"
        >
            立即充值
        </button>
    </view>
</template>

<script>
import { recharge } from '@/api/wallet'

export default {
    data() {
        return {
            amount: '',
            amountOptions: [50, 100, 200, 500, 1000],
            selectedPayment: 'wxpay',
            paymentMethods: [
                {
                    id: 'wxpay',
                    name: '微信支付',
                    icon: '/static/images/wallet/wxpay.png'
                },
                {
                    id: 'alipay',
                    name: '支付宝',
                    icon: '/static/images/wallet/alipay.png'
                }
            ],
            submitting: false
        }
    },
    computed: {
        isValid() {
            const amount = parseFloat(this.amount)
            return amount && amount >= 1
        }
    },
    methods: {
        selectAmount(amount) {
            this.amount = amount.toString()
        },
        selectPayment(id) {
            this.selectedPayment = id
        },
        async handleRecharge() {
            if (this.submitting) return
            
            try {
                this.submitting = true
                const res = await recharge({
                    amount: parseFloat(this.amount),
                    paymentMethod: this.selectedPayment
                })
                
                // 调用支付
                if (this.selectedPayment === 'wxpay') {
                    await uni.requestPayment(res)
                } else {
                    // 支付宝支付
                    // TODO: 实现支付宝支付
                }
                
                uni.showToast({
                    title: '充值成功',
                    icon: 'success'
                })
                
                setTimeout(() => {
                    uni.navigateBack()
                }, 1500)
            } catch (error) {
                uni.showToast({
                    title: error.message || '充值失败',
                    icon: 'none'
                })
            } finally {
                this.submitting = false
            }
        }
    }
}
</script>

<style lang="scss" scoped>
.container {
    min-height: 100vh;
    background-color: #f5f5f5;
    padding: 20rpx;
    
    .section-title {
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
        margin-bottom: 30rpx;
    }
    
    .amount-section {
        background-color: #fff;
        border-radius: 12rpx;
        padding: 30rpx;
        margin-bottom: 20rpx;
        
        .amount-input {
            display: flex;
            align-items: center;
            padding: 20rpx 0;
            border-bottom: 1rpx solid #eee;
            margin-bottom: 30rpx;
            
            .currency {
                font-size: 40rpx;
                color: #333;
                margin-right: 20rpx;
            }
            
            input {
                flex: 1;
                font-size: 40rpx;
            }
        }
        
        .amount-list {
            display: flex;
            flex-wrap: wrap;
            gap: 20rpx;
            
            .amount-item {
                width: calc((100% - 40rpx) / 3);
                height: 80rpx;
                line-height: 80rpx;
                text-align: center;
                background: #f5f5f5;
                border-radius: 8rpx;
                font-size: 28rpx;
                color: #333;
                
                &.active {
                    background: #07c160;
                    color: #fff;
                }
            }
        }
    }
    
    .payment-section {
        background-color: #fff;
        border-radius: 12rpx;
        padding: 30rpx;
        margin-bottom: 40rpx;
        
        .payment-list {
            .payment-item {
                display: flex;
                align-items: center;
                padding: 20rpx 0;
                
                &:not(:last-child) {
                    border-bottom: 1rpx solid #eee;
                }
                
                image {
                    width: 48rpx;
                    height: 48rpx;
                    margin-right: 20rpx;
                }
                
                text {
                    flex: 1;
                    font-size: 28rpx;
                    color: #333;
                }
                
                .check-icon {
                    width: 40rpx;
                    height: 40rpx;
                    border-radius: 50%;
                    border: 1rpx solid #ddd;
                    
                    &::after {
                        content: '';
                        display: none;
                        width: 20rpx;
                        height: 20rpx;
                        background: #07c160;
                        border-radius: 50%;
                        margin: 9rpx;
                    }
                }
                
                &.active {
                    .check-icon {
                        border-color: #07c160;
                        
                        &::after {
                            display: block;
                        }
                    }
                }
            }
        }
    }
    
    .submit-btn {
        width: 100%;
        height: 88rpx;
        line-height: 88rpx;
        background: #07c160;
        color: #fff;
        font-size: 32rpx;
        border-radius: 44rpx;
        
        &[disabled] {
            background: #ccc;
        }
    }
}
</style> 